package zhai.docking.alipay.util;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @package: zhai.docking.alipay.util
 * @project-name: zhai-docking-alipay
 * @description: 字符串工具类
 * @author: Created by 翟凤玺
 * @create-datetime: 2018-9-6 16:21
 */
public class StringUtil {

    /**
     * 是否为空
     */
    public static Boolean isBlank(String str) {
        return StringUtils.isBlank(str);
    }

    /**
     * 是否不为空
     */
    public static Boolean isNotBlank(String str) {
        return StringUtils.isNotBlank(str);
    }

    /**
     *  字符串左侧补充
     * @param str 被补充串
     * @param size 补充位数
     * @param padStr 补充串
     * @return
     */
    public static String leftPad(String str, Integer size, String padStr) {
        return StringUtils.leftPad(str, size, padStr);
    }

    /**
     *  字符串右侧补充
     * @param str 被补充串
     * @param size 补充位数
     * @param padStr 补充串
     * @return
     */
    public static String rightPad(String str, Integer size, String padStr) {
        return StringUtils.rightPad(str, size, padStr);
    }

    /**
     * 字符串合并
     */
    public static <T> String join(T... elements) {
        return StringUtils.join(elements);
    }

    public static boolean equalsAny(String str, String... searchStr) {
        return StringUtils.equalsAny(str, searchStr);
    }

    /**
     * 去除字符串所有空格
     */
    public static String trimAllSpace(String str) {
        return str.replace(" ", "");
    }

    /**
     * 字符串截取
     */
    public static String subStr(String str, Integer start, Integer end) {
        if (str.length() > end) {
            str = str.substring(start, end);
        }
        return str;
    }

    /**
     * list转字符串
     */
    public static <T> String listToStr(List<T> array, String separator) {
        return StringUtil.arrayToStr(array.toArray(), separator);
    }

    /**
     * 数组转字符串
     */
    public static <T> String arrayToStr(T[] array, String separator) {
        return StringUtils.join(array, separator);
    }

    /**
     * 字符串格式化
     */
    public static String format(String template, Object... args) {
        return String.format(template, args);
    }

    /**
     * 字符串拆分
     */
    public static List<String> split(String str, String regex) {
        return Arrays.stream(str.split(regex)).collect(Collectors.toList());
    }

    public static void main(String[] args) {
        System.out.println("字符串左侧补充：" + StringUtil.leftPad("A",6,"0"));
        System.out.println("字符串右侧补充：" + StringUtil.rightPad("A",6,"0"));
        System.out.println("是否包含某个字符串：" + StringUtil.equalsAny("aaaa","aaaa","b"));
        System.out.println("字符串去空：" + StringUtil.trimAllSpace(" a   a "));
        System.out.println("字符串格式化：" + StringUtil.format("{%s,%s}","A", 1));
        System.out.println("字符串截取：" + StringUtil.subStr("12 3abc", 1, 4));
        String[] a = {"A","B"};
        System.out.println("数组转字符串：" + StringUtil.arrayToStr(a, ":"));
        List<String> b = new ArrayList<>();
        b.add("A");
        b.add("B");
        System.out.println("List转字符串：" + StringUtil.listToStr(b, "?"));
    }
}
